package cn.yuanqiao.web.controller.cqbc;

import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.core.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.core.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.cqbc.yqCarrierManage.domain.YqCarrierManage;
import cn.yuanqiao.cqbc.yqCarrierManage.service.YqCarrierManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;


/**
 * 载体管理控制器
 *
 * @author lyk
 * @Date 2022-11-17 15:21:32
 */
@RestController
@RequestMapping("/cqbc/yqCarrierManage")
public class YqCarrierManageController extends BaseController {

    @Autowired
    private YqCarrierManageService yqCarrierManageService;

    /*@Autowired
    private EEPUtil eepUtil;

    @Autowired
    private ChangQiBaoCunCheckXML changQiBaoCunCheck;

    @Resource
    private YqCarriermanageDetailMapper yqCarriermanageDetailMapper;

    @Resource
    private DictMapper sysDictDao;*/

    /**
     * 新增接口
     *
     * @author lyk
     * @Date 2022-11-17
     */

    @Log(title = "新增载体", businessType = BusinessType.INSERT)
    @PostMapping("/addItem")
    public AjaxResult addItem(@RequestBody YqCarrierManage yqCarrierManageParam) {
        String code=yqCarrierManageParam.getCode();
        int num=yqCarrierManageService.getNumByCode(code);
        if(num>0){
            throw new ServiceException("载体编号不能重复！");
        }else {
            return toAjax(yqCarrierManageService.add(yqCarrierManageParam));
        }
    }

    /**
     * 编辑接口
     *
     * @author lyk
     * @Date 2022-11-17
     */
    @Log(title = "编辑载体", businessType = BusinessType.UPDATE)
    @PutMapping("/editItem")
    public AjaxResult editItem(@RequestBody YqCarrierManage yqCarrierManageParam) {
        //唯一验证
        String code=yqCarrierManageParam.getCode();
        int num=yqCarrierManageService.getNumByCode(code);
        //加一个判断
        YqCarrierManage detail = yqCarrierManageService.getById(yqCarrierManageParam.getId());
        if(detail.getCode().equals(code)){
            return toAjax(yqCarrierManageService.update(yqCarrierManageParam));
        }else{
            if(num>0){
                throw new ServiceException("载体编号不能重复！");
            }else {
                return toAjax(yqCarrierManageService.update(yqCarrierManageParam));
            }
        }
    }

    /**
     * 编辑接口
     *
     * @author lyk
     * @Date 2022-11-17
     */
    @Log(title = "载体检测", businessType = BusinessType.INSERT)
    @PostMapping("/detection")
    public AjaxResult detection(YqCarrierManage yqCarrierManageParam) {


        //yqDataManageParam.setCategory(maps.get("CategoryId").toString());
        //
        // this.yqCarrierManageService.update(yqCarrierManageParam);
        //return ResponseData.success();
        return toAjax(yqCarrierManageService.detection(yqCarrierManageParam));
    }



    /**
     * 删除接口
     *
     * @author lyk
     * @Date 2022-11-17
     */
    @Log(title = "删除载体", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam("id") Long id) {
        return toAjax(yqCarrierManageService.delete(id));
    }

    /**
     * 查看详情接口
     *
     * @author lyk
     * @Date 2022-11-17
     */
    @GetMapping(value = "/{ID}")
    @Log(title = "载体管理详情", businessType = BusinessType.SELECT)
    public AjaxResult detail(@PathVariable("ID") Long ID) {
        YqCarrierManage detail = yqCarrierManageService.getById(ID);
        return success(detail);
    }

    /**
     * 查询列表
     *
     * @author lyk
     * @Date 2022-11-17
     */
    @Log(title = "载体管理列表查询", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(YqCarrierManage yqCarrierManageParam) {
        startPage();
        List<YqCarrierManage> list = yqCarrierManageService.findListBySpec(yqCarrierManageParam);
        return getDataTable(list);
    }
}


